package com.yb.xczx.cloud.auth.filter;

import com.yb.core.constant.ServletRequestConstant;
import com.yb.core.enums.LoginTypeEnum;
import com.yb.core.model.XczxLoginUser;
import com.yb.xczx.cloud.auth.bean.XczxAbstractAuthenticationToken;
import com.yb.xczx.cloud.auth.bean.XczxSysUser;
import com.yb.xczx.cloud.auth.bean.XczxUserLoginCodeAuthenticationToken;
import com.yb.xczx.cloud.auth.entity.SysUser;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/17 16:02
 * FileName: XczxTokenAuthenticationFilter
 * Description:
 */

public class XczxUserLoginCodeAuthenticationFilter extends XczxAbstractAuthenticationProcessingFilter {
    private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER = new AntPathRequestMatcher("/login",
            "POST");


    private boolean postOnly = true;
    public XczxUserLoginCodeAuthenticationFilter() {
        super(DEFAULT_ANT_PATH_REQUEST_MATCHER);
    }

    public XczxUserLoginCodeAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(DEFAULT_ANT_PATH_REQUEST_MATCHER,authenticationManager);
    }

    @Override
    protected String loginType() {
        return LoginTypeEnum.LOGIN_CODE.getLoginType();
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        XczxLoginUser xczxLoginUser = (XczxLoginUser) request.getAttribute(ServletRequestConstant.XCZX_LOGIN_USER);
        String logincode = xczxLoginUser.getCode();
        String username = xczxLoginUser.getUsername();
        if (!StringUtils.hasText(logincode)) {
            logincode="";
        }
        if (!StringUtils.hasText(username)) {
            username="";
        }

        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword("*******************");
        XczxSysUser xczxSysUser = new XczxSysUser(sysUser);
        XczxUserLoginCodeAuthenticationToken unauthenticated = XczxUserLoginCodeAuthenticationToken.unauthenticated(xczxSysUser, logincode);

        setDetails(request, unauthenticated);

        return getAuthenticationManager().authenticate(unauthenticated);
    }

    protected void setDetails(HttpServletRequest request, XczxAbstractAuthenticationToken authRequest) {
        authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
    }

}
